package com.yilin.tms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.system.system.AppBanner;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.system.mapper.AppBannerMapper;
import com.yilin.tms.system.service.IAppBannerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * APP广告service
 *
 * @author zhangjinyuan
 * @date 2022/8/3
 */
@Service
@Transactional()
public class AppBannerService extends ServiceImpl<AppBannerMapper, AppBanner> implements IAppBannerService {

    @Resource
    private AppBannerMapper appBannerMapper;

    /**
     * 分页查询APP广告
     */
    @Override
    public PageData<AppBanner> getAppBannerPage(AppBanner query, Integer start, Integer limit) {
        return page(new PageData<>(start, limit), QueryUtil.andQueryWrapper(new QueryWrapper<>(),query));
    }

    /**
     * 获取上线中的广告集合
     */
    @Override
    public List<AppBanner> getReleaseAppBannerList() {
        QueryWrapper<AppBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("view_state", AppBanner.ViewState.release);
        return appBannerMapper.selectList(queryWrapper);
    }

    @Override
    public void saveAppBanner(Account loginUser, AppBanner appBanner) {
        appBanner.setViewState(AppBanner.ViewState.save);
        appBanner.setCreateTime(new Date());
        appBannerMapper.insert(appBanner);
    }

    @Override
    public void editAppBanner(Account loginUser, AppBanner appBanner, AppBanner localAppBanner) {
        if (StringUtil.isNotEmpty(appBanner.getTitle())) {
            localAppBanner.setTitle(appBanner.getTitle());
        }
        if (StringUtil.isNotEmpty(appBanner.getRemark())) {
            localAppBanner.setRemark(appBanner.getRemark());
        }
        if (StringUtil.isNotEmpty(appBanner.getImageUrl())) {
            localAppBanner.setImageUrl(appBanner.getImageUrl());
        }
        if (appBanner.getViewType() != null) {
            localAppBanner.setViewType(appBanner.getViewType());
        }
        if (StringUtil.isNotEmpty(appBanner.getFileUrl())) {
            localAppBanner.setFileUrl(appBanner.getFileUrl());
        }
        localAppBanner.setUpdateTime(new Date());
        localAppBanner.setUpdateUserId(loginUser.getId());
        appBannerMapper.updateById(localAppBanner);
    }

    /**
     * 上线/下线广告
     */
    @Override
    public boolean appBannerUpOrDown(Account loginUser, AppBanner localAppBanner) {
        boolean booln;
        if (localAppBanner.getViewState() == AppBanner.ViewState.save) {
            localAppBanner.setViewState(AppBanner.ViewState.release);
            booln = true;
        } else {
            localAppBanner.setViewState(AppBanner.ViewState.save);
            booln = false;
        }
        localAppBanner.setUpdateUserId(loginUser.getId());
        localAppBanner.setUpdateTime(new Date());
        appBannerMapper.updateById(localAppBanner);
        return booln;
    }

    /**
     * 删除广告
     */
    @Override
    public int removeAppBanner(Account loginUser, String[] appBannerIds) {
        int succesCount = 0;
        for (String appBannerId : appBannerIds) {
            AppBanner localAppBanner = appBannerMapper.selectById(appBannerId);
            if (localAppBanner != null) {
                appBannerMapper.deleteById(localAppBanner);
                succesCount++;
            }
        }
        return succesCount;
    }

    @Override
    public AppBanner getAppBannerById(String appBannerId) {
        return appBannerMapper.selectById(appBannerId);
    }


}
